package com.luis.toolbox.common;

import com.luis.toolbox.intf.MyTransformer;
import com.luis.toolbox.intf.PrepareHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.MessageChannels;
import org.springframework.messaging.MessageChannel;

@Configuration
public class ChannelConfigure {

	@Autowired
	private PrepareHandler prepareHandler;

	@Autowired
	private MyTransformer transformer;

	@Bean
	public MessageChannel handlerInboundChannel() {
		return MessageChannels.direct().get();
	}

	@Bean
	public MessageChannel mqttOutboundChannel() {
		return MessageChannels.direct().get();
	}

	@Bean
	public MessageChannel influx2OutboundChannel() {
		return MessageChannels.direct().get();
	}

	@Bean
	public MessageChannel httpOutboundChannel() {
		return MessageChannels.direct().get();
	}


	@Bean
	public MessageChannel consoleOutboundChannel() {
		return MessageChannels.direct().get();
	}


	@Bean
	@ConditionalOnProperty(prefix="outbound",name = "destination", havingValue = "influx2")
	public IntegrationFlow myFlow3(){
		return IntegrationFlows.from("handlerInboundChannel")
				.<String>filter(StringUtils::isNotBlank)
				//.channel("influx2OutboundChannel").get();
				.channel("consoleOutboundChannel").get();
	}

	@Bean
	@ConditionalOnProperty(prefix="outbound",name = "destination", havingValue = "rc")
	public IntegrationFlow myFlow2(){
		return IntegrationFlows.from("handlerInboundChannel")
				.handle(prepareHandler)
				.transform(transformer)
				.<String>filter(StringUtils::isNotBlank)
				.channel("mqttOutboundChannel").get();
	}

	@Bean
	@ConditionalOnProperty(prefix="outbound",name = "destination", havingValue = "http")
	public IntegrationFlow myFlow4(){
		return IntegrationFlows.from("handlerInboundChannel")
				.<String>filter(StringUtils::isNotBlank)
				.channel("httpOutboundChannel").get();
	}


	@Bean
	@ConditionalOnMissingBean(IntegrationFlow.class)
	public IntegrationFlow myFlow(){
		return IntegrationFlows.from("handlerInboundChannel")
				.handle(prepareHandler)
				.transform(transformer)
				.<String>filter(StringUtils::isNotBlank)
				.channel("consoleOutboundChannel").get();
	}



}
